package cn.tedu.boot31.controller;

import cn.tedu.boot31.mapper.EmpMapper;
import cn.tedu.boot31.vo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
    //此注解会创建一个实例了EmpMapper接口的类   并且实例化出一个对象
    @Autowired(required = false)
    EmpMapper mapper;



    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        mapper.insert(emp);
        return "添加完成!";
    }

    @RequestMapping("/select")
    public String select(){
        //调用Mapper里面的查询方法
        List<Emp> list = mapper.select();
        //将数据显示到表格中
        String html="<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>工作</th><th>工资</th><th>操作</th></tr>";
        //添加员工信息
        for (Emp emp : list) {//有几条信息取决于集合的长度
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            html+="<td>"+emp.getSal()+"</td>";
            html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }


    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除成功<a href='/select'>返回列表</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        mapper.update(emp);
        return "修改完成!<a href='/select'>返回列表表面</a>";
    }
}
